package com.hifar.sso.model;

import java.io.Serializable;
import java.util.Date;

/**
 * SSO认证响应模型
 *
 * @author system
 * @since 1.0.0
 */
public class SSOResponse implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * 响应码
     */
    private Integer code;

    /**
     * 响应消息
     */
    private String message;

    /**
     * 访问令牌
     */
    private String accessToken;

    /**
     * 刷新令牌
     */
    private String refreshToken;

    /**
     * 令牌类型
     */
    private String tokenType = "Bearer";

    /**
     * 过期时间
     */
    private Long expiresIn;

    /**
     * 作用域
     */
    private String scope;

    /**
     * 用户信息
     */
    private SSOUser user;

    /**
     * 重定向URL
     */
    private String redirectUrl;

    /**
     * 状态参数
     */
    private String state;

    /**
     * 错误代码
     */
    private String error;

    /**
     * 错误描述
     */
    private String errorDescription;

    /**
     * 响应时间
     */
    private Date timestamp;


    


    public SSOResponse() {
        this.timestamp = new Date();
    }

    public SSOResponse(Integer code, String message) {
        this();
        this.code = code;
        this.message = message;
    }

    /**
     * 成功响应
     */
    public static SSOResponse success(String message) {
        return new SSOResponse(200, message);
    }

    /**
     * 成功响应带数据
     */
    public static SSOResponse success(String message, SSOUser user, SSOToken token) {
        SSOResponse response = new SSOResponse(200, message);
        response.setUser(user);
        response.setAccessToken(token.getAccessToken());
        response.setRefreshToken(token.getRefreshToken());
        response.setExpiresIn(token.getExpiresIn());
        response.setScope(token.getScope());
        return response;
    }

    /**
     * 成功响应带令牌数据
     */
    public static SSOResponse success(String message, String userId, SSOToken token) {
        SSOResponse response = new SSOResponse(200, message);
        response.setAccessToken(token.getAccessToken());
        response.setRefreshToken(token.getRefreshToken());
        response.setExpiresIn(token.getExpiresIn());
        response.setScope(token.getScope());
        return response;
    }

    /**
     * 错误响应
     */
    public static SSOResponse error(Integer code, String message) {
        return new SSOResponse(code, message);
    }

    /**
     * 错误响应带错误代码
     */
    public static SSOResponse error(Integer code, String message, String error, String errorDescription) {
        SSOResponse response = new SSOResponse(code, message);
        response.setError(error);
        response.setErrorDescription(errorDescription);
        return response;
    }

    /**
     * 重定向响应
     */
    public static SSOResponse redirect(String redirectUrl, String state) {
        SSOResponse response = new SSOResponse(302, "Redirect");
        response.setRedirectUrl(redirectUrl);
        response.setState(state);
        return response;
    }

    // Getters and Setters
    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getAccessToken() {
        return accessToken;
    }

    public void setAccessToken(String accessToken) {
        this.accessToken = accessToken;
    }

    public String getRefreshToken() {
        return refreshToken;
    }

    public void setRefreshToken(String refreshToken) {
        this.refreshToken = refreshToken;
    }

    public String getTokenType() {
        return tokenType;
    }

    public void setTokenType(String tokenType) {
        this.tokenType = tokenType;
    }

    public Long getExpiresIn() {
        return expiresIn;
    }

    public void setExpiresIn(Long expiresIn) {
        this.expiresIn = expiresIn;
    }

    public String getScope() {
        return scope;
    }

    public void setScope(String scope) {
        this.scope = scope;
    }

    public SSOUser getUser() {
        return user;
    }

    public void setUser(SSOUser user) {
        this.user = user;
    }

    public String getRedirectUrl() {
        return redirectUrl;
    }

    public void setRedirectUrl(String redirectUrl) {
        this.redirectUrl = redirectUrl;
    }

    public String getState() {
        return state;
    }

    public void setState(String state) {
        this.state = state;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public String getErrorDescription() {
        return errorDescription;
    }

    public void setErrorDescription(String errorDescription) {
        this.errorDescription = errorDescription;
    }

    public Date getTimestamp() {
        return timestamp;
    }

    public void setTimestamp(Date timestamp) {
        this.timestamp = timestamp;
    }

    /**
     * 判断是否成功
     */
    public boolean isSuccess() {
        return code != null && code == 200;
    }

    /**
     * 判断是否需要重定向
     */
    public boolean isRedirect() {
        return code != null && code == 302;
    }

    @Override
    public String toString() {
        return "SSOResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", accessToken='" + accessToken + '\'' +
                ", refreshToken='" + refreshToken + '\'' +
                ", tokenType='" + tokenType + '\'' +
                ", expiresIn=" + expiresIn +
                ", scope='" + scope + '\'' +
                ", user=" + user +
                ", redirectUrl='" + redirectUrl + '\'' +
                ", state='" + state + '\'' +
                ", error='" + error + '\'' +
                ", errorDescription='" + errorDescription + '\'' +
                ", timestamp=" + timestamp +
                '}';
    }
}
